/** 
 * Project Name:kaka 
 * File Name:CompanyCardAction.java 
 * Package Name:com.luoran.kaka.api.wechat 
 * Date:2018年12月10日下午2:29:55 
 * Copyright (c) 2018, Luoran, Inc. All Rights Reserved.
 * 
 */  
package com.luoran.kaka.api.wechat;

import java.awt.image.PixelInterleavedSampleModel;
import java.io.File;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.alibaba.fastjson.JSONObject;
import com.luoran.kaka.entity.CompanyInfo;
import com.luoran.kaka.entity.PicInfo;
import com.luoran.kaka.service.IAboutInfoService;
import com.luoran.kaka.service.ICompanyInfoService;
import com.luoran.kaka.service.IPicInfoService;
import com.luoran.kaka.utils.UUID;

/** 
 * @author ghl 
 * @since JDK 1.8
 * ClassName: CompanyCardAction
 * date: 2018年12月10日 下午2:29:55
 *	
 */
@Controller
@RequestMapping("/api/company")
public class CompanyCardAction {
	private static final Logger log = LoggerFactory.getLogger(CompanyCardAction.class);
	
	@Autowired
	private Environment evn;
	@Autowired
	private IPicInfoService picInfoService;
	@Autowired
	private ICompanyInfoService companyInfoService;
	@Autowired
	private IAboutInfoService aboutInfoService;
	
	
	@RequestMapping("/upload")
	@ResponseBody
	public JSONObject doUpload(HttpServletRequest req) {
		JSONObject obj = new JSONObject();
		PicInfo pic = new PicInfo();
		MultipartHttpServletRequest multiparReq = (MultipartHttpServletRequest) req;
		Iterator<String> fileNames = multiparReq.getFileNames();
		if (fileNames.hasNext()) {
			try {
				MultipartFile file = multiparReq.getFile(fileNames.next());
				String originalName = file.getOriginalFilename();
				long size = file.getSize();
				String suffix = originalName.substring(originalName.lastIndexOf(".")).toLowerCase();// 原始后缀小写
				String path = evn.getProperty("file.path");// 获取图片存储目录
				String newFileName = UUID.get() + suffix;// 根据UUID生成新的文件名称带后缀
				String depositUrl = path + "/" + newFileName;
				File dest = new File(depositUrl);// 上传目标路径

				if (!dest.getParentFile().exists()) {
					dest.getParentFile().mkdirs();
					log.info("父及目录不存在，创建父及目录。");
				}
				file.transferTo(dest);// 图片上传
				pic.setOriginalName(originalName);
				pic.setSize((int) size);
				pic.setName(newFileName);
				pic.setDepositUrl(depositUrl);
				pic.setUrl(evn.getProperty("file.path.url") + req.getContextPath() + "/" + newFileName);// 根据配置的虚拟路径可以直接用url访问本地图片,正式上线可以通过nginx反向代理访问linux图片目录
				pic.setCreatetime(new Date());
				pic.setLastupdatetime(new Date());
				String picId = picInfoService.insertPic(pic);
				if (!StringUtils.isEmpty(picId)) {
					String url = pic.getUrl();
					obj.put("flag", true);
					obj.put("url", url);
					obj.put("picId", picId);
				}
			} catch (Exception e) {
				e.printStackTrace();
				obj.put("flag", false);
				obj.put("msg", "网络异常，请重试");
			}
		}
		return obj;
	}

	@RequestMapping("/card/save")
	@ResponseBody
	public JSONObject save(CompanyInfo company) {
		JSONObject obj=new JSONObject();
		company.setCreatetime(new Date());
		company.setLastupdatetime(new Date());
		String companyId=companyInfoService.addCompany(company);
		if(StringUtils.isEmpty(companyId)) {
			log.info("保存成功! id:"+companyId);
			obj.put("flag", false);
		}else{
			obj.put("flag", true);
			obj.put("companyId", companyId);
		}
		return obj;
	}
	
	@RequestMapping("/oneByid")
	@ResponseBody
	public JSONObject oneByid(HttpServletRequest resquest) {
		JSONObject obj=new JSONObject();
		String companyId=resquest.getParameter("id");
		if(!StringUtils.isEmpty(companyId)) {
			List<Object> aboutList=aboutInfoService.getAboutInfosById(companyId);
			CompanyInfo company = companyInfoService.getCompanyInfoById(companyId);
			PicInfo pic=picInfoService.getPicInfoById(company.getPicId());
			obj.put("pic", pic);
			obj.put("abouts", aboutList);
			obj.put("company", company);
			obj.put("flag", true);
		}else {
			obj.put("flag", false);
		}
		return obj;
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public JSONObject update(CompanyInfo company) {
		JSONObject obj = new JSONObject();
		int result=companyInfoService.updateById(company);
		if(result>0) {
			obj.put("flag", true);
		}else {
			obj.put("flag", false);
		}
		return obj;
	}
}
